home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / luff stuff / luff⁄makeautomat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-11-08  |  5.0 KB  |  200 lines  |  [TEXT/MACA]

  1. # include "stdio.h"
  2. # define FOUND 1
  3. # define NOT_FOUND 0
  4. # define DUMMY 'd'
  5.  
  6. empty(){
  7.     while(getchar() != '\n');
  8. }
  9.  
  10. fempty(fd) FILE *fd;{
  11.     while(getc(fd) != '\n');
  12. }
  13.  
  14. main(){
  15.     char text[128], uttext[128], c;
  16.     char textarray[512][16];
  17.     int automat[512][3]; /* .=0 O=1 X=2 */
  18.     char pattern[4];
  19.     int offs, offset[3], point[3], threat[3];
  20.     int p[10], t[10], count, length;
  21.     FILE *fd_in, *fd_out;
  22.     int i, j, k;
  23.     int flag;
  24.  
  25. /* ********** Pass 1 *********************************************************
  26. L{ser in en fil fr}n standard input med formatet:
  27. ---------
  28. |
  29. |
  30. | Block vilket inneh}ller po{ngdefenitioner.
  31. |
  32. |
  33. ---------
  34. |
  35. |
  36. | Block vilket inneh}ller m|nster vilka ger po{ng.
  37. |
  38. |
  39. ---------
  40. Utdata skrivs p} filen make_first och inneh}ller m|nster plus po{ng.
  41. *************************************************************************** */
  42.  
  43.     printf("Pass 1\n");
  44.     for(i = 0; i < 10; i++){
  45.         scanf("%*c%d%d", &t[i], &p[i]); empty();
  46.     }
  47.  
  48.     fd_out = fopen("make_first", "w");
  49.     while(scanf("%s", text) != EOF){
  50.         if(text[0] == 0)
  51.             continue;
  52.         j = 0;
  53.         for(i = 0; i < 3; i++){
  54.             offset[i] = 0;
  55.             point[i] = 0;
  56.             threat[i] = 0;
  57.         }
  58.         for(count = 0; text[count] != 0; count++);
  59.         for(i = 0; text[i] !=0; i++){
  60.             if((text[i] == '.') || (text[i] == 'X'))
  61.                 uttext[i] = text[i];
  62.             else{
  63.                 offset[j] = count - i - 1;
  64.                 threat[j] = t[text[i] - 'a'];
  65.                 point[j] = p[text[i] - 'a'];
  66.                 uttext[i] = '.';
  67.                 j++;
  68.             }
  69.         }
  70.         uttext[i] = 0;
  71.         fprintf(fd_out, "%-10s%3d", uttext, j);
  72.         for(i = 0; i < 3; i++)
  73.             fprintf(fd_out,"%5d%3d%3d", offset[i], threat[i], point[i]);
  74.         fprintf(fd_out,"\n");
  75.     }
  76.     fclose(fd_out);
  77.  
  78. /* ********** Pass 2 *********************************************************
  79. L{ser filen make_first samt l{gger till motsvarande m|nster med
  80. O i st}llet f|r X.
  81. *************************************************************************** */
  82.  
  83.     printf("Pass 2\n");
  84.     fd_in = fopen("make_first", "r");
  85.     fd_out = fopen("make_temp1", "w");
  86.     while((fscanf(fd_in, "%c", &c)) != EOF)
  87.         if(c == 'X')
  88.             fprintf(fd_out, "O");
  89.         else
  90.             fprintf(fd_out, "%c", c);
  91.     fclose(fd_in);
  92.     fclose(fd_out);
  93.     system("cat make_first make_temp1 > make_temp2");
  94.     system("mv make_temp2 make_first");
  95.     system("rm make_temp1");
  96.  
  97. /* ********** Pass 3 *********************************************************
  98. L{ser filen make_first samt l{gger till delm|nster vilka leder till m|nstren.
  99. Om delm|nstret redan finns skall det ej l{ggas till. D.v.s inga kopior
  100. skall uppst}. Delm|nstren ges po{ng noll. Filen sorteras.
  101. *************************************************************************** */
  102.  
  103.     printf("Pass 3\n");
  104.     fd_in = fopen("make_first", "r");
  105.     length = 0;
  106.     while((fscanf(fd_in, "%s", textarray[length])) != EOF){
  107.         length++;
  108.         fempty(fd_in);
  109.     }
  110.     count = length;
  111.     fclose(fd_in);
  112.     fd_out = fopen("make_first", "a");
  113.     for(i = 0; i < count; i++){
  114.         strcpy(text, textarray[i]);
  115.         for(; text[0] != 0; text[strlen(text) - 1] = 0){
  116.             flag = NOT_FOUND;
  117.             for(j = 0; j < length; j++)
  118.                 if(!strcmp(text, textarray[j])){
  119.                     flag = FOUND;
  120.                     break;
  121.                 }
  122.             if(flag == NOT_FOUND){
  123.                 strcpy(textarray[length++], text);
  124.                 fprintf(fd_out, "%-10s%3d", text, 0);
  125.                 for(k = 0; k < 3; k++)
  126.                     fprintf(fd_out, "%5d%3d%3d", 0, 0, 0);
  127.                 fprintf(fd_out, "\n");
  128.                 length++;
  129.             }
  130.         }
  131.     }
  132.     fclose(fd_out);
  133.     system("sort make_first -o make_sort");
  134.  
  135. /* ********** Pass 4 *********************************************************
  136. Det sista passet vilket konstruerar automaten samt g|r om filen till en
  137. c-fil med namnet automat.c.
  138. *************************************************************************** */
  139.  
  140.     printf("Pass 4\n");
  141.     fd_in = fopen("make_sort", "r");
  142.     strcpy(textarray[0], "");
  143.     length = 1;
  144.     while((fscanf(fd_in, "%s", textarray[length])) != EOF){
  145.         length++;
  146.         fempty(fd_in);
  147.     }
  148.     strcpy(pattern, ".OX");
  149.     for(i = 0; i < length; i++){
  150.         strcpy(text, textarray[i]);
  151.         for(j = strlen(text) + 1; j < 20; j++)
  152.             text[j] = 0;
  153.         text[strlen(text)] = DUMMY;
  154.         for(j = 0; j < 3; j++){
  155.             text[strlen(text) - 1] = pattern[j];
  156.             flag = NOT_FOUND;
  157.             for(offs = 0; offs < 11; offs++){
  158.                 for(k = 0; k < length; k++)
  159.                     if(!strcmp(text + offs, textarray[k])){
  160.                         flag = FOUND;
  161.                         automat[i][j] = k;
  162.                         break;
  163.                     }
  164.                 if(flag == FOUND)
  165.                     break;
  166.             }
  167.             if(flag == NOT_FOUND)
  168.                 printf("Cant find %s\n", text);
  169.         }
  170.     }
  171.     fclose(fd_in);
  172.     fd_in = fopen("make_sort", "r");
  173.     fd_out = fopen("make_automat", "w");
  174.     for(i = 0; i < 3; i++){
  175.         offset[i] = 0;
  176.         point[i] = 0;
  177.         threat[i] = 0;
  178.     }
  179.     count = 0;
  180.     for(i = 0; i < length; i++){
  181.         fprintf(fd_out, "/*%3d %-10s*/", i, textarray[i]);
  182.         fprintf(fd_out, "%3d,%3d,%3d,", automat[i][0], automat[i][1],
  183.             automat[i][2]);
  184.         fprintf(fd_out, "%5d,", count);
  185.         for(j = 0; j < 3; j++)
  186.             fprintf(fd_out, "%4d,%3d,%3d,", offset[j], threat[j],
  187.                 point[j]);
  188.         fprintf(fd_out, "\n");
  189.         fscanf(fd_in, "%*s%d", &count);
  190.         for(j = 0; j < 3; j++)
  191.             fscanf(fd_in, "%d%d%d\n", &offset[j], &threat[j],
  192.                 &point[j]);
  193.     }
  194.     fprintf(fd_out, "};\n");
  195.     fclose(fd_in);
  196.     fclose(fd_out);
  197.     system("cat make_header make_automat | sed 's/ 0,/ __/g' > automat.c");
  198.     system("rm make_first make_sort make_automat");
  199. }
  200.